package com.shenmazong.rand;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.RandomAccess;

/**
 * @program: server-java-demo
 * @description: ListRandomChoice
 * @author: 亮子说编程
 * @create: 2020-10-16 10:25
 **/
public class ListRandomChoice {

    public static void main(String[] args) {

        //-- 创建list
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 1000000; i++) {
            list.add("list"+i);
        }

        //-- 判断
        if(list instanceof RandomAccess) {
            //-- 随机访问
            for (int i = 0; i < list.size(); i++) {
                String msg = list.get(i);
            }
        }
        else {
            //-- 顺序访问
            Iterator<String> iterator = list.iterator();
            while (iterator.hasNext()) {
                String msg = iterator.next();
            }
        }
    }
}
